using System;
using System.Collections.Generic;
using System.Text;

namespace cb0tServer
{
    class CommandEval
    {
        public static CommandObject EvaluateClientCommandString(String cmdText)
        {
            CommandObject cmd = new CommandObject();
            cmd.cmdText = cmdText; // command text will be the full string no matter
            int first_space = cmdText.IndexOf(" ");
            if (first_space == -1) return cmd;
            cmdText = cmdText.Substring(first_space + 1); // removed command name
            if (cmdText.Length == 0) return cmd;

            cmd.tUser = UserPool.GetUserByName(cmdText); // right click

            if (cmd.tUser != null)
            {
                cmd.args = "";
                return cmd;
            }

            if (cmdText.Substring(0, 1) == "\"") // looking for name in quotes method
            {
                cmdText = cmdText.Substring(1);
                int second_quote = cmdText.IndexOf("\"");
                if (second_quote <= 0) return cmd;
                if (cmdText.Substring(0, second_quote).Length == 0) return cmd;
                cmd.tUser = UserPool.GetUserByNameStartsWith(cmdText.Substring(0, second_quote));
                cmdText = cmdText.Substring(second_quote + 1);
                if (cmdText.Length >= 2)
                {
                    cmd.args = cmdText.Substring(1);
                }
                else
                {
                    cmd.args = String.Empty;
                }
                return cmd;
            }
            else // looking for id method
            {
                int second_space = cmdText.IndexOf(" ");
                ushort eval_id;
                if (second_space <= 0) // no args
                {
                    if (!ushort.TryParse(cmdText, out eval_id)) return cmd;
                    cmd.tUser = UserPool.GetUserById(eval_id);
                    return cmd;
                }
                if (!ushort.TryParse(cmdText.Substring(0, second_space), out eval_id)) return cmd;
                cmd.tUser = UserPool.GetUserById(eval_id);
                cmdText = cmdText.Substring(second_space);
                if (cmdText.Length >= 2)
                {
                    cmd.args = cmdText.Substring(1);
                }
                else
                {
                    cmd.args = String.Empty;
                }
                return cmd;
            }
        }
    }

    class CommandObject
    {
        public String cmdText = String.Empty;
        public UserObject tUser = null;
        public String args = String.Empty;
    }

    class LinkedCommandObject
    {
        public LinkedUserObject lUser = null;
        public String args = String.Empty;
    }
}
